package com.ldzy.webase.service.impl;

import com.ldzy.webase.dao.SportsMapper;
import com.ldzy.webase.entity.Sports;
import com.ldzy.webase.entity.SportsOrder;
import com.ldzy.webase.entity.SportsTime;
import com.ldzy.webase.exception.ExceptionResponse;
import com.ldzy.webase.exception.ExceptionResponseCode;
import com.ldzy.webase.service.SportsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

/**
 * @author ：Lq
 * @version ：1.0
 * @date ：2022/11/15 17:36
 * @description ：t_sports-场馆预约表-业务层
 */
@Service
public class SportsServiceImpl implements SportsService {
    @Autowired
    SportsMapper sportsMapper;

    /**
     * @author ：Lq
     * @date ：2022/11/15 17:36
     * @description ：查询所有场馆信息，可根据场馆类型查询
     */
    @Override
    public List<Sports> getAllSports(String type) {
        List<Sports> sportsList = sportsMapper.getAllSports(type);

        if (sportsList == null || sportsList.size() == 0) {
            throw ExceptionResponse.build(ExceptionResponseCode.ERROR_NOT_DATA); // (401, "无数据")
        }

        return sportsList;
    }

    /**
     * @author ：Lq
     * @date ：2022/11/15 17:37
     * @description ：根据场馆id获取场馆详情信息
     */
    @Override
    public Sports getSportsBySportsId(Integer sportsId) {
        Sports sports = sportsMapper.getSportsBySportsId(sportsId);

        if (sports == null) {
            throw ExceptionResponse.build(ExceptionResponseCode.ERROR_NOT_TOKEN);// (401, "服务器开小差了")
        }

        return sports;
    }

    /**
     * @author ：Lq
     * @date ：2022/11/22 12:10
     * @description ：根据场馆id获取场馆的 预约时间-开始 至 预约时间-结束
     */
    @Override
    public List<SportsTime> getSportsStartEndDateBySportsId(Integer sportsId, Date orderDate) {
        List<SportsTime> sportsTimeList = sportsMapper.getSportsStartEndDateBySportsId(sportsId,orderDate);

        if (sportsTimeList.size() == 0) {
            throw ExceptionResponse.build(ExceptionResponseCode.ERROR_NOT_TOKEN);// (401, "服务器开小差了")
        }

        return sportsTimeList;
    }

    /**
     * @author ：Lq
     * @date ：2022/11/23 14:13
     * @description ：根据学生uid和场馆sportsId添加预约记录信息
     */
    @Override
    public int addSportsOrder(SportsOrder sportsOrder) {
        int i = sportsMapper.addSportsOrder(sportsOrder);

        if (i == 0) {
            throw ExceptionResponse.build(ExceptionResponseCode.ERROR_NOT_TOKEN);// (401, "服务器开小差了")
        }

        return i;
    }

    /**
     * @author ：Lq
     * @date ：2022/12/3 11:01
     * @description ：根据学生uid查询已预约的记录信息
     */
    @Override
    public List<SportsOrder> getSportsOrderByUid(Integer uid) {
        List<SportsOrder> sportsList = sportsMapper.getSportsOrderByUid(uid);
        return sportsList;
    }
}
